var runningSoftware = new Map();
var nowSoftware;


function openModal(obj) {

    var runningSoftwareInfo = {"modal": "", "taskBar": ""};
    if (runningSoftware.get($(obj).children("div.title").html()) == null) {
        var softwareName = $(obj).children("div.title").html();
        runningSoftwareInfo.modal = getModalName(softwareName);
        runningSoftwareInfo.taskBar = getTaskName(softwareName);
        /*移除之前的焦点 储存当前获得焦点的软件*/
        if (nowSoftware != null) {
            $("#" + runningSoftware.get(nowSoftware).taskBar).children("a").removeClass("active");
        }
        nowSoftware = softwareName;
        runningSoftware.set(softwareName, runningSoftwareInfo);
        appendModalHtml(runningSoftwareInfo.modal, softwareName, obj.dataset.url);
        $("#" + runningSoftware.get(nowSoftware).modal).css("z-index", 19970420);
        $("#" + runningSoftwareInfo.modal).modal('show');
        /*定义拖动*/
        $("#" + runningSoftwareInfo.modal).draggable({
            /*开始拖动就切换到该软件*/
            start: function () {
                switchSoftware(softwareName)
            },
            /*只能在桌面范围内拖动*/
            containment: ".softwares",
        });
        appendTaskHtml(softwareName, runningSoftwareInfo.taskBar);
    } else {
        $("#" + name).modal('show');
    }

}

function delModal(obj) {
    nowSoftware = null;
    $("#" + runningSoftware.get(obj).modal).remove();
    $("#" + runningSoftware.get(obj).taskBar).remove();
    runningSoftware.delete(obj);
}

function getModalName(name) {
    return "modal" + name;
}

function getTaskName(name) {
    return "TaskBarSoftware" + name;
}

function appendModalHtml(id, name, url) {
    var html = '<div class="modal" id="' + id + '" tabindex="-1"' +
        '     tabindex="0"  data-backdrop="false" data-keyboard="false"  style="left: 10%;top: 20px; width: 60%;height:80%;">' +
        '            <div onclick=switchSoftware("' + name + '") class="panel panel-inverse" style="height: 100%;" data-sortable-id="ui-media-object-1">' + /*panel-inverse黑色任务栏*/
        '                <div class="panel-heading">' +
        '                    <div class="panel-heading-btn">' +
        '                        <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-default"' +
        '                           data-click="panel-expand"><i class="fa fa-expand"></i></a>' +
        '                        <a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-warning"' +
        '                          onclick="panelMinimization(this)"><i class="fa fa-minus"></i></a>' +
        '                        <a href="javascript:;" onclick=delModal("' + name + '") class="btn btn-xs btn-icon btn-circle btn-danger"' +
        '                          ' +
        '                           aria-hidden="true"><i class="fa fa-times"></i></a>' +
        '                    </div>' +
        '                    <h4 class="panel-title">' + name + '</h4>' +
        '                </div>' +

        '                  <iframe id="' + name + '"  name="modalIframe"  frameborder="0" height="100%" width="100%" src="' + url + '"></iframe>' +

        '            </div>' +
        '</div>';
    $("#modals").append(html);
    /*给所有的modalIframe绑定点击事件*/
    iframeOnClick(name)
}

function iframeOnClick(name) {

    IframeOnClick.track(document.getElementById(name), function () {
        switchSoftware(name)
    });
}


/*iframe绑定点击事件*/
var IframeOnClick = {
    resolution: 200,
    iframes: [],
    interval: null,
    Iframe: function () {
        this.element = arguments[0];
        this.cb = arguments[1];
        this.hasTracked = false;
    },
    track: function (element, cb) {
        this.iframes.push(new this.Iframe(element, cb));
        if (!this.interval) {
            var _this = this;
            this.interval = setInterval(function () {
                _this.checkClick();
            }, this.resolution);
        }
    },
    checkClick: function () {
        if (document.activeElement) {
            var activeElement = document.activeElement;
            for (var i in this.iframes) {
                if (activeElement === this.iframes[i].element) {
                    if (this.iframes[i].hasTracked == false) {
                        this.iframes[i].cb.apply(window, []);
                        this.iframes[i].hasTracked = true;
                    }
                } else {
                    this.iframes[i].hasTracked = false;
                }
            }
        }
    }
};

/*添加底部应用栏*/
function appendTaskHtml(name, id) {
    var html = '<li id="' + id + '" onclick=switchSoftware("' + name + '")  class="nav-item"><a href="#nav-tab-4" data-toggle="tab" class="nav-link active">' + name + '</a></li>';

    $("#taskBarSoftware").children("li.next-button").before(html);
}

/*切换软件*/
function switchSoftware(name) {
    var id=runningSoftware.get(name).modal;
    /*宽为0则显示软件*/
    if($("#" + id).width()==0){
        /*获取最小化之前的位置和宽*/
        var left=$("#"+id).data("left")
        var top=$("#"+id).data("top")
        var width= $("#"+id).data("width");
        /*赋值*/
        $("#"+runningSoftware.get(name).modal).animate({left:left,width:width,top:top},100);
    }
    /*切换在上层软件和task高亮*/
    if (name != nowSoftware) {
        $("#" + runningSoftware.get(nowSoftware).taskBar).children("a").removeClass("active");
        $("#" + runningSoftware.get(nowSoftware).modal).css("z-index", 19970419);
        /*软件变更*/
        nowSoftware = name;
        $("#" + runningSoftware.get(nowSoftware).modal).css("z-index", 19970420);
        $("#" + runningSoftware.get(nowSoftware).taskBar).children("a").addClass("active");
    }
}

/*点击最小化按钮*/
function panelMinimization(obj) {
    /*获取到点击的哪一个模态框*/
    var id=obj.parentNode.parentNode.parentNode.parentNode.id;
    /*获取该模态框对应的Task的位置*/
    var left=$("#" + runningSoftware.get(nowSoftware).taskBar).offset().left;
    var top=$("#" + runningSoftware.get(nowSoftware).taskBar).offset().top;
    /*储存最小化之前modal的位置和宽*/
    $("#"+id).data("left",$("#"+id).offset().left);
    $("#"+id).data("top",$("#"+id).offset().top);
    $("#"+id).data("width",$("#"+id).css("width"));
    /*宽度变为0（隐藏）*/
    $("#"+id).animate({left:left,width:0,top:top},100);
}
